<template>
    <div class="login-container">
        <el-card class="login-card" shadow="hover">
            <template #header>
                <div class="login-title">用户登录</div>
            </template>
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-width="80px" class="login-form">
                <el-form-item label="用户名" prop="Username">
                    <el-input v-model="loginForm.Username" placeholder="请输入用户名" :prefix-icon="User"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="Password">
                    <el-input v-model="loginForm.Password" placeholder="请输入密码" :prefix-icon="Lock"
                        show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-switch v-model="loginForm.Isremember" class="ml-2" inline-prompt
                        style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text="记住密码"
                        inactive-text="忘记密码" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleLogin" :loading="isLoading" class="login-button" full-width>
                        登录
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElForm, ElFormItem, ElInput, ElButton, ElCheckbox, ElCard } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import httpaxios from '@/AxiosHelp'
import { useCounterStore } from '@/stores/counter'
import { useRouter } from 'vue-router'
const router = useRouter();
const pinia = useCounterStore();
// 登录表单数据
const loginForm = reactive({
    Username: '',
    Password: '',
    Isremember: false
})
// 表单验证规则
const loginRules = reactive({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
    ]
})
// 表单引用
const loginFormRef = ref<InstanceType<typeof ElForm>>()
// 加载状态
const isLoading = ref(false)
// 处理登录
const handleLogin = () => {
    httpaxios.get("/api/Users/UserLogin", {
        params: {
            UserName: loginForm.Username,
            UserPwd: loginForm.Password
        }
    }).then(res => {
        if (res.data.Code == 200) {
            localStorage.setItem("IsLogin","已登录")
            ElMessage.success(res.data.Message)

            // 登录判定
        // 登录成功，写JWT的 token到本地
        window.localStorage.setItem("token", res.data.Data.ToKen);
        // 数据提交后端要加入新的Token值
        httpaxios.defaults.headers.common["Authorization"] =
            "Bearer " + res.data.Data.ToKen;
            //是否记住密码
            if (loginForm.Isremember == true) {
                pinia.UserInfo.UserName = loginForm.Username;
                pinia.UserInfo.UserPwd = loginForm.Password;
            }
            else {
                pinia.UserInfo.UserName = "";
                pinia.UserInfo.UserPwd = "";
            }
            //持久化用户信息
            pinia.UserInfo.UserName = res.data.Data.UserName;
            pinia.UserInfo.RoleName = res.data.Data.RoleName;
            pinia.UserInfo.ToKen = res.data.Data.ToKen;
            router.push("/Main")

        }
    })
}
//反填密码
const Outway = () => {
    if (pinia.UserInfo.UserName != "") {
        loginForm.Username = pinia.UserInfo.UserName;
        loginForm.Password = pinia.UserInfo.UserPwd;
        loginForm.Isremember = true;
    }

}
// 组件挂载时，检查是否有保存的登录信息
onMounted(() => {
    Outway();
})
</script>

<!-- 折叠的样式部分 -->
<style scoped>
.login-container {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #87CEEB;
}

.login-card {
    width: 400px;
    background-color: #fff;
}

.login-title {
    font-size: 24px;
    font-weight: 600;
    color: #333;
    text-align: center;
}

.login-form {
    margin-top: 20px;
}

.login-button {
    margin-top: 10px;
}

/* 响应式设计 */
@media (max-width: 480px) {
    .login-card {
        width: 90%;
        margin: 20px;
    }
}
</style>